package cn.iocoder.yudao.module.yyzx.service.retreat;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.yyzx.controller.admin.retreat.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.retreat.RetreatDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.yyzx.dal.mysql.retreat.RetreatMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.yyzx.enums.ErrorCodeConstants.*;

/**
 * 退住登记 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RetreatServiceImpl implements RetreatService {

    @Resource
    private RetreatMapper retreatMapper;

    @Override
    public Integer createRetreat(RetreatSaveReqVO createReqVO) {
        // 插入
        RetreatDO retreat = BeanUtils.toBean(createReqVO, RetreatDO.class);
        retreatMapper.insert(retreat);
        // 返回
        return retreat.getId();
    }

    @Override
    public void updateRetreat(RetreatSaveReqVO updateReqVO) {
        // 校验存在
        validateRetreatExists(updateReqVO.getId());
        // 更新
        RetreatDO updateObj = BeanUtils.toBean(updateReqVO, RetreatDO.class);
        retreatMapper.updateById(updateObj);
    }

    @Override
    public void deleteRetreat(Integer id) {
        // 校验存在
        validateRetreatExists(id);
        // 删除
        retreatMapper.deleteById(id);
    }

    private void validateRetreatExists(Integer id) {
        if (retreatMapper.selectById(id) == null) {
            throw exception(RETREAT_NOT_EXISTS);
        }
    }

    @Override
    public RetreatDO getRetreat(Integer id) {
        return retreatMapper.selectById(id);
    }

    @Override
    public PageResult<RetreatDO> getRetreatPage(RetreatPageReqVO pageReqVO) {
        return retreatMapper.selectPage(pageReqVO);
    }

}